package com.hrpmar.utils;

import com.hrpmar.pojo.BasePojo;
import com.hrpmar.pojo.Worker;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by 李松松 on 2017/11/18.
 */
@Aspect
public class BasePojoAdvice {

    @Before("execution(* com.hrpmar.service.impl.*.add*Aspect(..))")
    public void before(JoinPoint jp){
        BasePojo basePojo = null;
        Object[] params = jp.getArgs();
        if(params!=null&&params.length>0){
            for(Object param:params){
                if(param instanceof BasePojo){
                    basePojo=(BasePojo) param;
                    HttpSession session=SystemContext.getHtppSession();
                    Worker loginUser=(Worker) session.getAttribute(Constants.LOGIN_USER);
                    basePojo.setCreatedBy(loginUser.getWoId());
                    basePojo.setCreationDate(new Date());
                }
            }
        }
        if(basePojo!=null){

        }
    }
    @Before("execution(* com.hrpmar.service.impl.*.update*Aspect(..))")
    public void UpdateBefore(JoinPoint jp){
        BasePojo basePojo = null;
        Object[] params = jp.getArgs();
        if(params!=null&&params.length>0){
            for(Object param:params){
                if(param instanceof BasePojo){
                    basePojo=(BasePojo) param;
                    HttpSession session=SystemContext.getHtppSession();
                    Worker loginUser=(Worker) session.getAttribute(Constants.LOGIN_USER);
                    basePojo.setModifyBy(loginUser.getWoId());
                    basePojo.setModifyDate(new Date());
                }
            }
        }
    }

}
